#include "detailwnd.h"
#include <QtGui>
#include "wndmanager.h"
#include "syscontroller.h"

DetailWnd::DetailWnd(int item_index, QWidget *parent)
	: QWidget(parent),
	  sys_(SysController::instance()),
	  item_index_(item_index)
{	
	ui.setupUi(this);

	if (sys_->IsEsdItem(item_index_)) {
		ui.dccFrame->setVisible(true);
		ui.hueFrame->setVisible(false);
		
		ui.ref1Combo->addItems(sys_->RefItemList(item_index_));
		ui.ref2Combo->addItems(sys_->RefItemList(item_index_));
		ui.ref1Combo->setCurrentIndex(
				ui.ref1Combo->findText(sys_->esdRefItem(item_index_, 0)));
		ui.ref2Combo->setCurrentIndex(
				ui.ref1Combo->findText(sys_->esdRefItem(item_index_, 1)));
	} else {
		ui.hueFrame->setVisible(true);
		ui.dccFrame->setVisible(false);

		ui.autoCalibCheck->setChecked(sys_->hueAutoRange(item_index_));
		ui.ofstCancelCheck->setChecked(sys_->hueOfstCancel(item_index_));

		ui.range1Combo->addItems(sys_->RangeList(3));
		ui.range2Combo->addItems(sys_->RangeList(3));
		ui.gain1Combo->addItems(sys_->RangeList(4));
		ui.gain2Combo->addItems(sys_->RangeList(4));
		ui.range1Combo->setCurrentIndex(
				ui.range1Combo->findText(sys_->huePreAmpRng(item_index_, 0)));
		ui.range2Combo->setCurrentIndex(
				ui.range2Combo->findText(sys_->huePreAmpRng(item_index_, 1)));
		ui.gain1Combo->setCurrentIndex(
				ui.gain1Combo->findText(sys_->hueAmpScf(item_index_, 0)));
		ui.gain2Combo->setCurrentIndex(
				ui.gain2Combo->findText(sys_->hueAmpScf(item_index_, 1)));

		if (!sys_->IsDlmItem(item_index_)) {
			ui.dlmFrame->setVisible(false);
		} else {
			ui.range3Combo->addItems(sys_->RangeList(3));
			ui.gain3Combo->addItems(sys_->RangeList(4));
			ui.range3Combo->setCurrentIndex(
					ui.range3Combo->findText(sys_->huePreAmpRng(item_index_, 2)));
			ui.gain3Combo->setCurrentIndex(
					ui.gain3Combo->findText(sys_->hueAmpScf(item_index_, 2)));
		}
	}
	
	connect(ui.okButton, SIGNAL(clicked()), this, SLOT(wndShouldReturn()));
	connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(wndShouldReturn()));
}


DetailWnd::~DetailWnd()
{
	delete ui.okButton;
	delete ui.cancelButton;
	delete ui.label;
	delete ui.label1;
	delete ui.label2;
	delete ui.label3;
	delete ui.label4;
	delete ui.label5;
	delete ui.label6;
	delete ui.label7;
	delete ui.autoCalibCheck;
	delete ui.ofstCancelCheck;
	delete ui.range1Combo;
	delete ui.range2Combo;
	delete ui.range3Combo;
	delete ui.gain1Combo;
	delete ui.gain2Combo;
	delete ui.gain3Combo;
	delete ui.ref1Combo;
	delete ui.ref2Combo;
	delete ui.dlmFrame;
	delete ui.dccFrame;
	delete ui.hueFrame;
}



void DetailWnd::wndShouldReturn()
{
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	if (button == ui.okButton) {
		SaveDetailSetting();
	} 
	WndManager::gotoWnd(WndManager::kWndProgramWnd);
}


void DetailWnd::SaveDetailSetting()
{
	if (sys_->IsEsdItem(item_index_)) {
		sys_->setEsdRefItem(item_index_, 0, ui.ref1Combo->currentText());
		sys_->setEsdRefItem(item_index_, 1, ui.ref2Combo->currentText());
	} else {
		sys_->setHueAutoRange(item_index_, ui.autoCalibCheck->isChecked());
		sys_->setHueOfstCancel(item_index_, ui.ofstCancelCheck->isChecked());
		sys_->setHuePreAmpRng(item_index_, 0, ui.range1Combo->currentText());
		sys_->setHuePreAmpRng(item_index_, 1, ui.range2Combo->currentText());
		sys_->setHueAmpScf(item_index_, 0, ui.gain1Combo->currentText());
		sys_->setHueAmpScf(item_index_, 1, ui.gain2Combo->currentText());
		if (sys_->IsDlmItem(item_index_)) {
			sys_->setHuePreAmpRng(item_index_, 2, ui.range3Combo->currentText());
			sys_->setHueAmpScf(item_index_, 2, ui.gain3Combo->currentText());
		}
	}
}
